package com.example.vieweventdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

/**
 * Created by tangjiangxu on 9/26/17.
 */

public class ThreadSleepActivity extends Activity{
    private TextView tv;

    private int mCount = 0;
    private static final int TOTAL = 100;
    private static final int DISTANCE = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_sleep);
        tv = (TextView) findViewById(R.id.tv);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (mCount < TOTAL) {
                    mCount ++;
                    float fraction = mCount / (float) TOTAL;
                    final int scrollX = (int) (fraction * DISTANCE);
                    final int scrollY = (int) (fraction * DISTANCE);
                    ThreadSleepActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv.scrollTo(scrollX, scrollY);
                        }
                    });
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }
}
